<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="题干:">
              <el-input
                clearable
                v-model="where.title"
                placeholder="请输入题干"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <el-form-item label="所属题库:">
              <el-select
                v-model="where.topic_id"
                clearable
                filterable
                placeholder="请选择所属题库"
              >
                <el-option
                  v-for="item in topicOption"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
                >查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            @click="removeBatch"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            size="small"
            v-if="permission.includes('sys:examp:dall')"
            >批量删除
          </el-button>
          <el-button
            @click="openImport()"
            type="success"
            icon="el-icon-top"
            class="ele-btn-icon"
            size="small"
            v-if="permission.includes('sys:examp:import')"
            >导入题目
          </el-button>
        </template>
        <!-- 状态列 -->
        <template slot="status" slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            @change="editStatus(row)"
            :active-value="1"
            :inactive-value="2"
          />
        </template>
        <template slot="problem_img" slot-scope="{ row }">
          <el-image
            :src="row.problem_img"
            :preview-src-list="[row.problem_img]"
            style="height: 40px;"
          >
          </el-image>
        </template>
        <template slot="type" slot-scope="{ row }">
          <el-tag :type="row.type | typeTagFilter" size="mini" effect="dark">{{
            row.type | typeFilter
          }}</el-tag>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:examp:edit')"
            >修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此题目吗？"
            @confirm="remove(row)"
          >
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:examp:delete')"
              >删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <examp-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"
    />
    <examp-import :visible.sync="showImport" v-if="showImport" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ExampEdit from "./examp-edit";
import ExampImport from "./examp-import";

export default {
  name: "Examp",
  components: { ExampEdit, ExampImport },
  computed: {
    ...mapGetters(["permission"]),
  },
  filters: {
    // 状态过滤   <!-- 用户状态 1 启用 2 冻结 3 注销 -->
    statusFilter(status) {
      const statusMap = {
        1: "启用",
        2: "禁用",
      };
      return statusMap[status];
    },
    // 考试类型 1 考试题目 2 练习题目
    typeFilter(status) {
      const statusMap = {
        1: "考试题目",
        2: "练习题目",
      };
      return statusMap[status];
    },
    // 状态类型过滤
    typeTagFilter(type) {
      const statusMap = {
        1: "success",
        2: "danger",
        3: "warning ",
      };
      return statusMap[type];
    },
  },
  data() {
    return {
      // 表格数据接口
      url: "/examp/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center",
          fixed: "left",
        },
        {
          prop: "id",
          label: "编号",
          width: 60,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left",
        },
        {
          prop: "title",
          label: "题干",
          align: "center",
          minWidth: 160,
        },
        {
          prop: "topic_name",
          label: "所属题库",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: "prescription_name",
          label: "所属处方",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: "problem_img",
          label: "处方图",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 60,
          slot: "problem_img",
        },
        {
          prop: "score",
          label: "分值",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: "right_key",
          label: "正确答案",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 100,
        },
        {
          prop: "status",
          label: "状态",
          align: "center",
          minWidth: 100,
          resizable: false,
          slot: "status",
        },
        {
          prop: "create_time",
          label: "录入时间",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 120,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          },
        },
        {
          prop: "update_time",
          label: "更新时间",
          align: "center",
          showOverflowTooltip: true,
          minWidth: 120,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          },
        },
        {
          columnKey: "action",
          label: "操作",
          width: 130,
          align: "center",
          resizable: false,
          slot: "action",
          fixed: "right",
        },
      ],
      // 表格搜索条件
      where: {
        type: "",
      },
      // 表格选中数据
      selection: [],
      topicOption: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示导入弹窗
      showImport: false,
    };
  },

  // 页面创建时
  created() {
    this.topicList();
  },
  methods: {
    /* 获取题库列表 */
    topicList() {
      this.$http
        .get("/topic/getExamTopicSelectList")
        .then((res) => {
          if (res.data.code === 0) {
            this.topicOption = res.data.data;
          }
        })
        .catch((e) => {
          this.$message.error(e.message);
        });
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {
        type: "",
      };
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = { ...row, t: +new Date() };
      this.showEdit = true;
    },
    /* 显示导入 */
    openImport() {
      this.showImport = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/examp/delete", { id: row.id })
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的题目吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/examp/delete", { id: this.selection.map((d) => d.id) })
            .then((res) => {
              loading.close();
              if (res.data.code === 0) {
                this.$message({ type: "success", message: res.data.msg });
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    /* 更改状态 */
    editStatus(row) {
      const loading = this.$loading({ lock: true });
      let params = Object.assign({
        id: row.id,
        status: row.status,
      });
      this.$http
        .post("/examp/status", params)
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message({ type: "success", message: res.data.msg });
          } else {
            row.status = !row.status ? 1 : 2;
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
  },
};
</script>
<style scoped></style>
